<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="联系人名称" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:customerData:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:customerData:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:customerData:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:customerData:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="56" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="82" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <!-- <el-table-column label="联系人名称" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="phone" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:customerData:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:customerData:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDetails(scope.row)"
            v-hasPermi="['system:customerData:remove']">查看明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改客户数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <!-- <el-form-item label="联系人名称" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人名称" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看分段列表抽屉 -->
    <!-- :close-on-click-modal="false" -->
    <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false" size="80%">
      <!-- div -->
      <div class="drawerStyle">
        分析内容
      </div>
      <div style="padding:0 24px;" v-for="(item, index) in tableData" :key="index" > 
        <div style="display: flex;align-items: center;justify-content: space-between;">
          <p><span class="oldSpan backStyle">{{ item.aiDeepseekAnswerTotalList[2].content }}</span></p>
          <div style="display: flex;align-items: center;justify-content: flex-end;">
            <p v-for="(itemtt, indextt) in textAry" :key="indextt" style="margin:0 !important;">
              <span v-if="itemtt.dui">
                <span class="spanStyleSizeSuccess backStyleSize">
                  {{ itemtt.text }} <i class="istyle">{{ itemtt.dui }}</i>
                </span>
              </span>
              <span v-else>
                <span class="spanStyleSize backStyleSize">
                  {{ itemtt.text }} <i class="istyleT">{{ itemtt.cuo }}</i>
                </span>
              </span>
            </p>
          </div>
        </div>
        <div class="descript">
          <div class="contentP">
            <p>交流摘要:</p>{{ item.aiDeepseekAnswerTotalList[12].content ? item.aiDeepseekAnswerTotalList[12].content : '-' }}
          </div>
          <p class="spanStyle backStyle" style="margin-top:24px">{{ item.aiDeepseekAnswerTotalList[0].content }}沟通</p>
        </div>
        <div style="display: flex;align-items: center;justify-content: space-between;">
          <div class="adBgcol">
            <p><span>流失风险</span><span style="font-size:26px;">{{ item.aiDeepseekAnswerTotalList[3].content }}</span></p>
            <p><span>流失风险说明</span><span>{{ item.aiDeepseekAnswerTotalList[4].content }}</span></p>
          </div>
          <div class="adBgcol">
            <p><span>成交意愿</span><span style="font-size:26px;">{{ item.aiDeepseekAnswerTotalList[5].content }}</span></p>
            <p><span>成交意愿说明</span><span>{{ item.aiDeepseekAnswerTotalList[6].content }}</span></p>
          </div>
        </div>
        <div>
          <p><span class="styleCol">所在行业：</span><span class="huag">{{ item.aiDeepseekAnswerTotalList[8].content }}</span>
          </p>
        </div>
        <div>
          <p><span class="styleCol">核心诉求：</span><span class="huag">{{ item.aiDeepseekAnswerTotalList[9].content }}</span>
          </p>
        </div>
        <div>
          <p><span class="styleCol">业务痛点：</span><span class="huag">{{ item.aiDeepseekAnswerTotalList[10].content }}</span>
          </p>
        </div>
        <div>
          <p><span class="styleCol">成交阻碍因素：</span><span class="huag">{{ item.aiDeepseekAnswerTotalList[11].content
              }}</span>
          </p>
        </div>
        <div>
          <p><span class="styleCol">产品：</span><span class="huag">{{ item.aiDeepseekAnswerTotalList[11].content }}</span>
          </p>
        </div>
        <div>
          <p class="huagongSize"><span style="margin-right:8px;">{{ item.aiDeepseekAnswerTotalList[7].content }}</span></p>
        </div>
      </div>

      <!-- 音频部分 -->
      <!-- <div class="drawerStyle">
        录音记录
      </div> -->
      <div class="drawerBtmStyle">
        <!-- <div class="customerStyle">
          <span>客户名称：{{ this.customerName }}</span>
          <span>时间：{{ this.time }}</span>
        </div> -->
        <div v-for="(item, index) in tableData" :key="index">
          <div v-for="(item1, index1) in item.aiRecordingDuanList" :key="index1">
            <!-- <el-table ref="multipleTable" :data="item1" tooltip-effect="dark" style="width: 100%" row-key="id" v-if="item1.aiRecordingChildList.length != 0"
              :tree-props="{ children: 'aiRecordingChildList', hasChildren: 'hasChildren' }">
              <el-table-column type="selection" width="55">
              </el-table-column>
              <el-table-column prop="rl" label="发言人" width="120" align="center">
              </el-table-column> -->
              <!-- <el-table-column label="音频播放" width="120">
            <template slot-scope="scope">音频地址</template>
          </el-table-column> -->
              <!-- <el-table-column prop="transcript" label="文字" width="362" style="display: flex;align-items: center;"
                align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.transcript }}</span>
                  <audio controls style="height: 36px;min-width:120px;"
                    v-if="!scope.row.aiRecordingChildList || scope.row.aiRecordingChildList.length === 0">
                    <source :src="loadPath + scope.row.filePath" type="audio/mpeg">
                  </audio>
                </template>
              </el-table-column> -->
              <!-- <el-table-column prop="customerName" label="客户名称">
            <template slot-scope="scope" v-if="scope.row.childList && scope.row.childList.length > 0">
              <span>{{ scope.row.customerName }}</span>
            </template>
          </el-table-column> -->
              <!-- <el-table-column prop="spearName" label="说话人" align="center">
                <template slot-scope="scope"
                  v-if="!scope.row.aiRecordingChildList || scope.row.aiRecordingChildList.length === 0">
                  <el-tag>{{ scope.row.spearName }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="takec" label="音频时长" show-overflow-tooltip align="center">
              </el-table-column>
              <el-table-column prop="bgc" label="片段开始时间" show-overflow-tooltip align="center">
              </el-table-column>
              <el-table-column prop="egc" label="片段结束时间" show-overflow-tooltip align="center">
              </el-table-column> -->
            <!-- </el-table> -->
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listCustomerData, getCustomerData, delCustomerData, addCustomerData, updateCustomerData, getCusInfo } from "@/api/system/customerData";
import { listDuan, getDuan, delDuan, addDuan, updateDuan } from "@/api/system/duan";

export default {
  name: "CustomerData",
  data() {
    return {
      time: '',
      customerName: '',
      textAry: [
        {
          value: 1,
          text: '讲公司',
          // dui: '✓',
          // cuo: 'X'

        },
        {
          value: 2,
          text: '讲产品',
          // dui: '✓',
          // cuo: 'X'
        },
        // {
        //   value: 3,
        //   text: '讲案例',
        //   // dui: '✓',
        //   // cuo: 'X'
        // }, {
        //   value: 4,
        //   text: '询问联系方式',
        //   // dui: '✓',
        //   // cuo: 'X'
        // }, 
        {
          value: 5,
          text: '邀请访厂',
          // dui: '✓',
          // cuo: 'X'
        }, {
          value: 6,
          text: '送样品',
          // dui: '✓',
          // cuo: 'X'
        }, {
          value: 7,
          text: '询问需求',
          // dui: '✓',
          // cuo: 'X'
        }, {
          value: 8,
          text: '添加量',
          // dui: '✓',
          // cuo: 'X'
        }, {
          value: 9,
          text: '添加成本',
          // dui: '✓',
          // cuo: 'X'
        }, {
          value: 10,
          text: '讲客户案例',
          // dui: '✓',
          // cuo: 'X'
        }, {
          value: 11,
          text: '催促下单',
          // dui: '✓',
          // cuo: 'X'
        },
      ],
      loadPath: process.env.VUE_APP_BASE_API,
      tableData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      drawer: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户数据表格数据
      customerDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        contactName: null,
        phone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 拿到客户明细
    handleDetails(row) {
      // 查看详情
      getCusInfo(row.id).then(res => {
        let dataT = []
        var ary = []
        dataT.push(res.data)
        dataT.forEach((item, index) => {
          item.id = index + 1
          item.aiRecordingDuanList.forEach(itemT => {
            itemT.aiRecordingChildList.forEach(item2 => {
              item2.bgc = item2.bg / 1000 + 's'
              item2.egc = item2.eg / 1000 + 's'
              item2.takec = item2.take / 1000 + 's'
            })
          })
          item.aiDeepseekAnswerTotalList.forEach(item3 => {
            if (item3.typeName.includes('关键词')) {
              let cc = item3.content.split(',')
              ary = cc
            }
          })
        })
        this.textAry.forEach(item => {
          ary.forEach((item2) => {
            if (item.text == item2) {
              item.dui = '✓'
            } else {
              item.cuo = 'X'
            }
          })
        })
        this.time = dataT[0].time;
        this.customerName = dataT[0].customerName;
        this.tableData = dataT
      })
      this.drawer = true
    },

    /** 查询客户数据列表 */
    getList() {
      this.loading = true;
      listCustomerData(this.queryParams).then(response => {
        this.customerDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        contactName: null,
        phone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomerData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomerData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomerData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户数据编号为"' + ids + '"的数据项？').then(function () {
        return delCustomerData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/customerData/export', {
        ...this.queryParams
      }, `customerData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.drawerStyle {
  padding: 24px;
  font-weight: bold;
}

.drawerBtmStyle {
  padding: 0 24px 24px 24px;
}

.btnEle {
  margin-top: 24px;
}

.divStyle {
  padding: 24px 0;
}

.divStyle .el-button {
  padding: 14px 46px;
  font-size: 18px;
}

.backStyle {
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 16px;
}

.oldSpan {
  background: #e8f4ff;
  border: 1px solid #d1e9ff;
  color: #1890ff;
  margin-right: 12px;
}

.highSpan {
  background: #e7faf0;
  border: 1px solid #d0f5e0;
  color: #13ce66;
}

.descript {
  border: 1px solid #e6ebf5;
  border-radius: 8px;
  padding: 12px;
}

.descript p {
  margin: 0 !important;
}

.contentP {
  color: #333;
  line-height: 32px;
}

.spanStyle {
  background: #ffdbda;
  color: #ff000c;
  border: 1px solid #f5d0d8;
  display: inline-block;
}

.styleCol {
  color: #333;
  display: inline-block;
}

.huag {
  color: #606266;
  margin-left: 24px;
}

.adBgcol {
  background: #eff6fe;
  display: flex;
  align-items: flex-start;
  padding: 12px;
  margin-top: 12px;
  border-radius: 8px;
  flex: 1;
  margin-right: 12px;
}

.adBgcol P {
  padding: 12px;
  display: flex;
  align-items: flex-start;
  flex-direction: column;
  padding: 0;
  margin: 0;
  color: #606266;
}

.adBgcol P:nth-child(1) {
  margin-right: 32px;
}

.adBgcol P span:nth-child(1) {
  margin-bottom: 12px;
}

.huagongSize {
  color: #1890ff;
  font-size: 14px;
}

.adBgcol P:nth-child(2) {
  font-size: 14px;
}

.spanStyleSize {
  background: #ffdbdac5;
  color: #ff000c;
  border: 1px solid #f5d0d8;
}

.backStyleSize {
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 12px;
  margin-right: 6px;
  display: flex;
  align-items: center;
}

.spanStyleSizeSuccess {
  background: #d6f7e9;
  color: #186d47;
  border: 1px solid #e2f5d0;
}

.backStyleSize i {
  border-radius: 100%;
  display: inline-block;
  width: 14px;
  height: 14px;
  font-size: 8px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 4px;
  padding-top: 2px;
  font-style: normal;
}

.istyle {
  border: 1px solid #186d47;
}

.istyleT {
  border: 1px solid red;
}

.customerStyle {
  display: flex;
  padding: 0 24px;
  font-size: 14px;
  color: #606266;
  margin-bottom: 24px;
}

.customerStyle span {
  margin-right: 24px;
}
</style>